﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.bandits {
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Classes.effects.*;
    import Modules.workers.*;
    import Display.*;
    import Display.CopsBanditsUI.*;
    import Classes.actions.*;
    import Engine.*;
    import com.zynga.skelly.util.*;

    public class ArrestScene extends Prop {

        public static const ARREST_STATES:Array = ["arrest_1", "arrest_2", "arrest_3"];
        protected static const ARREST_ITEM_NAME:String = "prop_arrest_scene";
        protected static const ARREST_RADIUS:int = 2;
        protected static const ARREST_LOITER_TIME:int = 6;
        protected static const ARREST_FADE_TIME:Number = 0.5;
        protected static const ARREST_TIME:Number = 6;

        public var m_cops:Array;
        protected var m_bandit:NPC;
        protected var m_arrestingCop:String = "";
        protected var m_numCars:int = 1;
        protected var m_data:BanditData;
        protected var m_alreadyStarted:Boolean = false;
        protected var m_alreadyFinished:Boolean = false;
        protected var m_cashScene:Boolean;
        protected var m_dooberCounter:int = 0;

        public function ArrestScene(_arg1:BanditData, _arg2:Vector3=null, _arg3:Boolean=false){
            super(ARREST_ITEM_NAME);
            this.m_data = _arg1;
            this.m_cashScene = _arg3;
            var _local4:Vector3 = ((_arg2)!=null) ? _arg2 : this.findRandomArrestPosition();
            this.setPosition(_local4.x, _local4.y);
        }
        public function get data():BanditData{
            return (this.m_data);
        }
        public function get numCars():int{
            return (this.m_numCars);
        }
        public function set numCars(_arg1:int):void{
            this.m_numCars = Math.max(_arg1, 0);
        }
        public function get bandit():NPC{
            return (this.m_bandit);
        }
        public function set bandit(_arg1:NPC):void{
            this.m_bandit = _arg1;
            if (this.m_bandit){
                this.m_bandit.clearStates();
            };
        }
        function init():void{
            this.createNpcs();
        }
        function shutdown():void{
            this.killNpcs();
            this.m_cops.splice(0);
        }
        function killNpcs():void{
            var npc:* = null;
            for each (npc in this.m_cops.concat(this.m_bandit)) {
                if (((!((npc is Vehicle))) && (!((npc == null))))){
                    if ((((npc == this.m_bandit)) && (npc))){
                        this.killBandit();
                    } else {
                        npc.getStateMachine().addActions(new ActionFn(npc, Curry.curry(function (_arg1:NPC):void{
                            _arg1.animation = "static";
                        }, npc)), new ActionNavigateRandom(npc).setTimeout(ArrestScene.ARREST_LOITER_TIME), new ActionTween(npc, ActionTween.TO, ArrestScene.ARREST_FADE_TIME, {alpha:0}), new ActionFn(npc, Curry.curry(function (_arg1:NPC):void{
                            Global.world.citySim.npcManager.removeWalker(_arg1);
                        }, npc)));
                    };
                };
            };
            this.m_bandit = null;
        }
        function killBandit():void{
            if (this.m_bandit){
                this.m_bandit.playActionCallback = null;
                if (Global.world.citySim.npcManager.isNpcTracked(this.m_bandit)){
                    Global.world.citySim.npcManager.removeNpc(this.m_bandit);
                } else {
                    this.m_bandit.detach();
                    this.m_bandit.cleanUp();
                };
                this.m_bandit = null;
            };
        }
        override public function getDepthPriority():Number{
            return (-1);
        }
        override protected function calculateDepthIndex():void{
            super.calculateDepthIndex();
            m_depthIndex = (m_depthIndex + 10000);
        }
        override public function getToolTipHeader():String{
            return (null);
        }
        override public function getProgressBarOffset():Point{
            return (new Point(0, -25));
        }
        protected function createNpcs():void{
            var cpos:* = null;
            var cop:* = null;
            var copDir:* = 0;
            var colliders:* = null;
            var collider:* = null;
            var newTile:* = null;
            var diff:* = null;
            if (this.bandit){
                this.setPosition(this.bandit.getPosition().x, this.bandit.getPosition().y);
            };
            var pos:* = this.getPosition();
            var bpos:* = this.getPosition();
            this.m_cops = [];
            var dir:* = Constants.DIRECTION_SW;
            var radialCount:* = Math.PI;
            var radialIncrement:* = ((2 * Math.PI) / this.m_data.requiredCops);
            var copNum:* = 0;
            while (copNum < this.m_data.requiredCops) {
                cop = BanditManager.pullCopForArrest(pos, this.m_cashScene);
                if (cop){
                    this.m_cops.push(cop);
                    cpos = new Vector3((pos.x + (ArrestScene.ARREST_RADIUS * Math.cos(radialCount))), (pos.y + (ArrestScene.ARREST_RADIUS * Math.sin(radialCount))));
                    copDir = this.getGoodCopArrestDirection(radialCount);
                    colliders = Global.world.getObjectsByClassAt(MapResource, cpos);
                    for each (collider in colliders) {
                        if ((((collider is Sidewalk)) || ((collider is Road)))){
                        } else {
                            newTile = Global.world.citySim.roadManager.findClosestWalkableTile(cpos);
                            cpos = newTile.getHotspot();
                            diff = cpos.subtract(pos);
                            copDir = this.getGoodCopArrestDirection(Math.atan2(diff.y, diff.x));
                            break;
                        };
                    };
                    if (copDir != -1){
                        cop.setDirection(copDir);
                    };
                    radialCount = (radialCount + radialIncrement);
                    if (copNum == 0){
                        this.m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Arresting"), ArrestScene.ARREST_TIME).setTimedCallback((ArrestScene.ARREST_TIME - 2.5), this.startBanditCaptureAnimation));
                    };
                    cop.animation = "idle";
                    cop.getStateMachine().addActions(new ActionTween(cop, ActionTween.TO, ArrestScene.ARREST_FADE_TIME, {alpha:0}), new ActionFn(cop, Curry.curry(function (_arg1:NPC, _arg2:Vector3):void{
                        _arg1.setPosition(_arg2.x, _arg2.y);
                        _arg1.conditionallyReattach();
                    }, cop, cpos)), new ActionAnimationEffect(cop, EffectType.POOF), new ActionFn(cop, Curry.curry(function (_arg1:NPC, _arg2:int):void{
                        _arg1.animation = ((_arg2 == -1)) ? "idle" : MathUtil.randomElement(ARREST_STATES);
                    }, cop, copDir)), new ActionFn(cop, this.startCaptureScene));
                    cop.actionSelection = new ArrestActionSelection(cop, true, ((copDir == -1)) ? "idle" : MathUtil.randomElement(ARREST_STATES));
                    cop.playActionCallback = null;
                };
                copNum = (copNum + 1);
            };
            if (!this.bandit){
                this.m_bandit = new NPC(BanditManager.NPC_BANDIT, false);
                this.m_bandit.setOuter(Global.world);
                this.m_bandit.attach();
            };
            this.m_bandit.actionSelection = new ArrestActionSelection(this.m_bandit, false, MathUtil.randomElement(ARREST_STATES));
            this.m_bandit.setPosition(bpos.x, bpos.y);
            this.m_bandit.setDirection(dir);
            this.m_bandit.playActionCallback = null;
        }
        protected function canArrestProgressStart():Boolean{
            return (true);
        }
        protected function cancelArrestProgress():void{
        }
        protected function startCaptureScene():void{
            var colliders:* = null;
            var dropPos:* = null;
            var dropOffTile:* = null;
            var collider:* = null;
            var cop:* = 0;
            var type:* = null;
            var actions:* = null;
            var loopsound:* = null;
            if (this.m_alreadyStarted){
                return;
            };
            this.m_alreadyStarted = true;
            Sounds.play("arrest");
            var nearRoad:* = null;
            var startRoad:* = null;
            var level:* = BanditUtil.getPoliceStationLevel();
            var car:* = null;
            if ((((level >= 6)) || (this.m_cashScene))){
                colliders = Global.world.getObjectsByClassAt(MapResource, getPosition());
                dropOffTile = null;
                for each (collider in colliders) {
                    if ((((collider is Sidewalk)) || ((collider is Road)))){
                        dropOffTile = Global.world.citySim.roadManager.findClosestWalkableTile(collider.getPosition(), function (_arg1):Boolean{
                            return (!((collider == _arg1)));
                        });
                        break;
                    };
                };
                if (!dropOffTile){
                    dropPos = getPosition();
                } else {
                    dropPos = dropOffTile.getHotspot();
                };
                BanditManager.sendHelicopter(dropPos, this.m_cashScene, null);
            } else {
                if (level >= 3){
                    cop = 0;
                    while (cop < this.m_numCars) {
                        type = ((level == 3)) ? BanditManager.NPC_POLICECRUISER : ((level == 4)) ? BanditManager.NPC_POLICEVAN : BanditManager.NPC_POLICESPORTCRUISER;
                        car = new Vehicle(type, false);
                        this.m_cops.push(car);
                        if (car){
                            nearRoad = Global.world.citySim.roadManager.findClosestRoad(getPosition());
                            if (!nearRoad){
                            } else {
                                startRoad = Global.world.citySim.roadManager.findRandomRoadOnScreen(nearRoad);
                                car.setPosition(startRoad.getHotspot().x, startRoad.getHotspot().y);
                                Global.world.citySim.npcManager.addVehicle(car);
                                actions = car.getStateMachine();
                                actions.removeAllStates();
                                actions.addActions(new ActionAnimationEffect(car, EffectType.VEHICLE_POOF), new ActionFn(car, function ():void{
                                    Sounds.play("siren");
                                    loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                                }), new ActionNavigate(car, nearRoad, startRoad).setPathType(RoadManager.PATH_ROAD_ONLY), new ActionFn(car, function ():void{
                                    Sounds.stop(loopsound);
                                }), new ActionPause(car, 2), new ActionFn(car, function ():void{
                                    loopsound = Sounds.play("tourbus_engine_loop", 0, Sounds.LOOPING);
                                }), new ActionNavigate(car, startRoad, nearRoad), new ActionFn(car, function ():void{
                                    Sounds.stop(loopsound);
                                }), new ActionFn(car, Curry.curry(function (_arg1:Vehicle):void{
                                    Global.world.citySim.npcManager.removeVehicle(_arg1);
                                }, car)), new ActionDie(car));
                            };
                        };
                        cop = (cop + 1);
                    };
                };
            };
        }
        protected function startBanditCaptureAnimation():void{
            var _local1:String = MathUtil.randomElement(ARREST_STATES);
            this.m_bandit.animation = "";
            this.m_bandit.actionQueue.addActions(new ActionPlayAnimationOneLoop(this.m_bandit, _local1), new ActionFn(this.m_bandit, this.killBandit));
        }
        protected function finishCaptureProgress():void{
            if (this.m_alreadyFinished){
                return;
            };
            this.m_alreadyFinished = true;
            this.finishCaptureScene();
        }
        protected function finishCaptureScene():void{
            var _local4:CopData;
            var _local1:NPC = this.m_cops[0];
            var _local2:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            if (_local2){
                _local4 = _local2.getNpcOwner(_local1);
                if (_local4){
                    this.m_arrestingCop = Global.player.getFriendName(GameUtil.formatServerUid(_local4.m_ZID));
                } else {
                    this.m_arrestingCop = ZLoc.t("Main", "FakeFriendName");
                };
            };
            if ((((this.m_arrestingCop == null)) || ((this.m_arrestingCop == "")))){
                this.m_arrestingCop = ZLoc.t("Main", "FakeFriendName");
            };
            this.killNpcs();
            m_doobersArray = this.makeDoobers();
            if (m_doobersArray.length > 0){
                this.spawnDoobers(true);
            };
            var _local3:Array = this.m_data.rewardItems;
            if (_local3.length > 0){
                this.giveItemRewards(_local3);
            };
            BanditUtil.refreshPoliceStationAppearance();
        }
        override public function spawnDoobers(_arg1:Boolean=false):void{
            GlobalEngine.log("Doobers", ("MapResource.spawnDoobers() on " + this.getItemName()));
            Global.world.dooberManager.createBatchDoobers(m_doobersArray, null, m_position.x, m_position.y, _arg1, this.showDialogCallback);
        }
        protected function showDialogCallback():void{
            if (Global.isVisiting()){
                return;
            };
            this.showCapturedDialog();
            BanditManager.removeArrestScene(this);
        }
        protected function showCapturedDialog(_arg1:Boolean=false, _arg2:Boolean=false):void{
            var _local6:String;
            var _local7:String;
            var _local8:CharacterDialog;
            var _local3:CopsNBanditsWorkers = (Global.copsNBanditsWorkerManager.getWorkers("cops") as CopsNBanditsWorkers);
            if (!_local3){
                return;
            };
            var _local4:CopData = _local3.getNpcOwner(this.m_cops[0]);
            var _local5:BanditCapturedDialog = new BanditCapturedDialog(this.m_data.id, _local4, _arg1, _arg2, this.showFeedHandler);
            UI.displayPopup(_local5);
            if (!Global.player.getSeenFlag("banditCapture")){
                Global.player.setSeenFlag("banditCapture");
                _local6 = Global.gameSettings.getString("CNBcatchBanditFTUEAsset");
                _local7 = ZLoc.t("Dialogs", "CapturedBandit_FTUE_message");
                _local8 = new CharacterDialog(_local7, "CapturedBandit", GenericDialogView.TYPE_OK, null, null, true, _local6, "OkButton");
                UI.displayPopup(_local8);
            };
        }
        protected function showFeedHandler(_arg1:GenericPopupEvent):void{
            if (_arg1.button == GenericPopup.YES){
                Global.world.viralMgr.sendBanditCapture(this.m_data.id, this.m_arrestingCop);
            };
        }
        protected function findRandomArrestPosition():Vector3{
            var _local1:Road = this.findBestRandomRoad();
            if (_local1 != null){
                return (_local1.getPosition());
            };
            return (new Vector3(0, 0));
        }
        protected function getGoodCopArrestDirection(_arg1:Number):int{
            var _local2:Number = (((_arg1 % (2 * Math.PI)) * 180) / Math.PI);
            if ((((_local2 <= 45)) || ((_local2 >= 315)))){
                return (Constants.DIRECTION_NE);
            };
            if (_local2 <= 135){
                return (Constants.DIRECTION_NW);
            };
            if (_local2 <= 225){
                return (Constants.DIRECTION_SW);
            };
            if (_local2 <= 315){
                return (Constants.DIRECTION_SE);
            };
            return (-1);
        }
        protected function findBestRandomRoad():Road{
            var _local3:Road;
            var _local5:Array;
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local1:Array = [];
            var _local2:Array = Global.world.getObjectsByClass(Road);
            for each (_local3 in _local2) {
                _local5 = _local3.adjacentRoads;
                if ((((_local5 == null)) || ((_local5.length == 0)))){
                } else {
                    _local6 = 0;
                    _local7 = _local5.length;
                    _local8 = 0;
                    while (_local8 < _local7) {
                        if (_local5[_local8] != null){
                            _local6++;
                        };
                        _local8++;
                    };
                    if ((((_local6 >= 3)) && (!(this.isRoadAlreadyUsed(_local3.getPosition()))))){
                        _local1.push(_local3);
                    };
                };
            };
            if (_local1.length > 0){
                return (MathUtil.randomElement(_local1));
            };
            var _local4:Road = Global.world.citySim.roadManager.findRandomRoad();
            return (_local4);
        }
        protected function isRoadAlreadyUsed(_arg1:Vector3):Boolean{
            var _local2:ArrestScene;
            var _local3:Vector3;
            for each (_local2 in BanditManager.getArrestScenes()) {
                _local3 = _local2.getPosition();
                if ((((_local3.x == _arg1.x)) && ((_local3.y == _arg1.y)))){
                    return (true);
                };
            };
            return (false);
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            return (this.m_data.doobers);
        }
        override public function getProgressBarStartFunction():Function{
            return (this.canArrestProgressStart);
        }
        override public function getProgressBarEndFunction():Function{
            return (this.finishCaptureProgress);
        }
        override public function getProgressBarCancelFunction():Function{
            return (this.cancelArrestProgress);
        }
        protected function giveItemRewards(_arg1:Array):void{
            var _local2:String;
            for each (_local2 in _arg1) {
                Global.player.addGift(_local2, 0, true);
            };
            if (_arg1.length > 0){
                this.showCapturedDialog(true);
            };
        }

    }
}//package Modules.bandits 
